//
//  YHSelectedTableCell.m
//  FAKER
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHSelectedTableCell.h"
#import "AllHeader.h"
#import "FitHeader.h"

@implementation YHSelectedTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
        [self changeColor];
    }
    return self;
}

#pragma mark - 创建视图
- (void)createViews
{
    _headImageView = [[BaseClassImageView alloc] init];
    _headImageView.layer.cornerRadius = 35 * FITWIDTH / 2;
    [CONTENT_VIEW addSubview:_headImageView];
    
    _usernameLabel = [[UILabel alloc] init];
    _usernameLabel.font = [UIFont systemFontOfSize:15];
    _usernameLabel.textColor = [[UIButton buttonWithType:UIButtonTypeSystem] titleColorForState:UIControlStateNormal];
    [CONTENT_VIEW addSubview:_usernameLabel];
    
    _postTimeLabel = [[UILabel alloc] init];
//    _postTimeLabel.textColor = [UIColor grayColor];
    _postTimeLabel.font = [UIFont systemFontOfSize:13];
    [CONTENT_VIEW addSubview:_postTimeLabel];
    
    _topicButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [_topicButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _topicButton.titleLabel.font = [UIFont systemFontOfSize:13];
    _topicButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_topicButton addTarget:self action:@selector(clickTopicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [CONTENT_VIEW addSubview:_topicButton];
//    _topicButton.backgroundColor = [UIColor redColor];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:15];
    _contentLabel.numberOfLines = 0;
    [CONTENT_VIEW addSubview:_contentLabel];
    
    for (NSInteger i = 0; i < 3; i++) {
//        BaseClassImageView *imageView = [[BaseClassImageView alloc] init];
//        [CONTENT_VIEW addSubview:imageView];
        
        switch (i) {
            case 0:
//                _coverImageView1 = imageView;
                _coverImageView1 = self.coverImageView1;
                break;
            case 1:
//                _coverImageView2 = imageView;
                _coverImageView2 = self.coverImageView2;
                break;
            case 2:
//                _coverImageView3 = imageView;
                _coverImageView3 = self.coverImageView3;
                break;
                
            default:
                break;
        }
    }
    _imageNumLabel = [[UILabel alloc] init];
    _imageNumLabel.font = [UIFont systemFontOfSize:13];
    _imageNumLabel.backgroundColor = [UIColor blackColor];
    _imageNumLabel.textColor = [UIColor whiteColor];
    _imageNumLabel.textAlignment = NSTextAlignmentCenter;
    _imageNumLabel.clipsToBounds = YES;
    [CONTENT_VIEW addSubview:_imageNumLabel];
    
    _visitCountField = [[UITextField alloc] init];
//    _visitCountField.textColor = [UIColor grayColor];
    _visitCountField.font = [UIFont systemFontOfSize:13];
    _visitCountField.leftViewMode = UITextFieldViewModeAlways;
    _visitCountField.enabled = NO;
    [CONTENT_VIEW addSubview:_visitCountField];
}
- (BaseClassImageView *)coverImageView1
{
    if (!_coverImageView1) {
        BaseClassImageView *imageView = [[BaseClassImageView alloc] init];
        [CONTENT_VIEW addSubview:imageView];
        return imageView;
    }
    return _coverImageView1;
}
- (BaseClassImageView *)coverImageView2
{
    if (!_coverImageView2) {
        BaseClassImageView *imageView = [[BaseClassImageView alloc] init];
        [CONTENT_VIEW addSubview:imageView];
        return imageView;
    }
    return _coverImageView2;
}
- (BaseClassImageView *)coverImageView3
{
    if (!_coverImageView3) {
        BaseClassImageView *imageView = [[BaseClassImageView alloc] init];
        [CONTENT_VIEW addSubview:imageView];
        return imageView;
    }
    return _coverImageView3;
}

#pragma mark - 重写属性set
#pragma mark 重写Selected
- (void)setYhSelected:(YHSelected *)yhSelected
{
    //保护数据不丢失
    if (yhSelected == nil) {
        return;
    }
    _yhSelected = yhSelected;
    
    //布局
    [self yhLayoutSubviews];
    
    [_headImageView sd_setImageWithURL:[_yhSelected.auther objectForKey:@"icon"] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    _postTimeLabel.text = _yhSelected.date;
    
    //设置发布之间label右侧topicLabel显示或隐藏
    if ([_yhSelected.special_info objectForKey:@"discussion_title"] != nil) {
        [_topicButton setTitle:[NSString stringWithFormat:@"# %@", [_yhSelected.special_info objectForKey:@"discussion_title"]] forState:UIControlStateNormal];
        _topicButton.alpha = 1;
    }
    else {
        _topicButton.alpha = 0;
    }
    
    _usernameLabel.text = [_yhSelected.auther objectForKey:@"name"];
    _contentLabel.text = _yhSelected.content;
    
    //给imageView赋图
    switch (_yhSelected.medias.count) {
        case 0:
            break;
            
        case 3:
            [_coverImageView3 sd_setImageWithURL:[_yhSelected.medias[2] objectForKey:@"url"] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
        case 2:
            [_coverImageView2 sd_setImageWithURL:[_yhSelected.medias[1] objectForKey:@"url"] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
        case 1:
        default:
            [_coverImageView1 sd_setImageWithURL:[_yhSelected.medias[0] objectForKey:@"url"] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
            break;
    }
    //设置图片个数
    if (_yhSelected.medias.count > 3) {
        _imageNumLabel.text = [NSString stringWithFormat:@"%ld", (unsigned long)_yhSelected.medias.count];
        _imageNumLabel.alpha = 1;
    }
    else {
        _imageNumLabel.alpha = 0;
    }
    
    //设置浏览量显示形式
    if ([_yhSelected.hot_num doubleValue] > 999) {
        _visitCountField.text = [NSString stringWithFormat:@" %.1fK", [_yhSelected.hot_num doubleValue] / 1000];
    }
    else {
        _visitCountField.text = [NSString stringWithFormat:@" %@", _yhSelected.hot_num];
    }
    
    [self getPassImageURL];
}

#pragma mark - 布局
- (void)yhLayoutSubviews
{
    [super layoutSubviews];
    _headImageView.frame = CGRectMake(15 * FITWIDTH, 15 * FITHEIGHT, 35 * FITWIDTH, 35 * FITWIDTH);
    _usernameLabel.frame = CGRectMake(_headImageView.X + _headImageView.WIDTH + _headImageView.X, _headImageView.Y, SCREEN_WIDTH - _headImageView.X * 3 - _headImageView.WIDTH, _headImageView.WIDTH / 2);
    _postTimeLabel.frame = CGRectMake(_usernameLabel.X, _usernameLabel.Y + _usernameLabel.HEIGHT, _usernameLabel.WIDTH / 2, _usernameLabel.HEIGHT);
    _topicButton.frame = CGRectMake(_postTimeLabel.X + _postTimeLabel.WIDTH, _postTimeLabel.Y - 10 * FITHEIGHT, _postTimeLabel.WIDTH, _postTimeLabel.HEIGHT + 20 * FITHEIGHT);
    
    //调整content的高度
    if (_contentHeight > 54.701172) {
        _contentHeight = 54.701172;
    }
    _contentLabel.frame = CGRectMake(_headImageView.X, _headImageView.Y + _headImageView.HEIGHT + 30 * FITHEIGHT, SCREEN_WIDTH - _headImageView.X * 2, _contentHeight);
    
    //布局图片
    [self yhLayoutCoverImageView];
    
    _visitCountField.frame = CGRectMake(SCREEN_WIDTH - _coverImageView1.X - 80 * FITWIDTH, _coverImageView1.Y + _coverImageView1.HEIGHT + 15 * FITHEIGHT, 80 * FITWIDTH, _topicButton.HEIGHT);
    UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    leftView.image = [UIImage imageNamed:@"Eye-gray@2x"];
    leftView.contentMode = UIViewContentModeTop;
    _visitCountField.leftView = leftView;
}
#pragma mark 布局coverImageView
- (void)yhLayoutCoverImageView
{
    CGFloat imageWidth = _contentLabel.WIDTH;
    CGFloat imageHeight1 = 270 * FITHEIGHT;
    CGFloat imageHeight2 = 0;
    CGFloat imageHeight3 = 0;
    switch (_yhSelected.medias.count) {
        case 0:
            imageHeight1 = 0;
            break;
        case 2:
            imageWidth = (_contentLabel.WIDTH - 5 * FITWIDTH) / 2;
            imageHeight1 = imageWidth;
            imageHeight2 = imageWidth;
            break;
        case 3:
            imageWidth = (_contentLabel.WIDTH - 10 * FITWIDTH) / 3;
            imageHeight1 = imageWidth;
            imageHeight2 = imageWidth;
            imageHeight3 = imageWidth;
            break;
            
        case 1:
        default:
            break;
    }
    
    //修改图片frame
    _coverImageView1.frame = CGRectMake(_contentLabel.X, _contentLabel.Y + _contentLabel.HEIGHT + 15 * FITHEIGHT, imageWidth, imageHeight1);
    _coverImageView2.frame = CGRectMake(_coverImageView1.X + _coverImageView1.WIDTH + 5, _coverImageView1.Y, _coverImageView1.WIDTH, imageHeight2);
    _coverImageView3.frame = CGRectMake(_coverImageView2.X + _coverImageView2.WIDTH + 5, _coverImageView2.Y, _coverImageView2.WIDTH, imageHeight3);
    
    _imageNumLabel.frame = CGRectMake(_coverImageView1.X + _coverImageView1.WIDTH - 30 * FITWIDTH, _coverImageView1.Y + _coverImageView1.HEIGHT - 30 * FITWIDTH, 20 * FITWIDTH, 20 * FITWIDTH);
    _imageNumLabel.layer.cornerRadius = _imageNumLabel.WIDTH / 2;
}

- (void)getPassImageURL
{
    _urlString = [[_yhSelected.special_info objectForKey:@"discussion_info_url"] componentsSeparatedByString:@"="].lastObject;
    _urlString = [NSString stringWithFormat:@"http://dis.myzaker.com/api/get_post.php?discussion_id=%@", _urlString];
    [InternetDataRequest getDataByString:_urlString BodyDic:nil WithDataBlock:^(id data) {
        _passImageURL = [[[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"discussion_info"] objectForKey:@"pic"];
    }];
}

#pragma mark - 点击topicButton方法
- (void)clickTopicButtonAction:(UITapGestureRecognizer *)tap
{
    YHTopicViewController *topicVC = [[YHTopicViewController alloc] init];
    topicVC.topicName = [_yhSelected.special_info objectForKey:@"discussion_title"];
    topicVC.urlString = _urlString;
    topicVC.coverImageURL = _passImageURL;
    
    //推出topicVC
//    topicVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:topicVC animated:YES completion:nil];
    
    //找到社区nav
    topicVC.hidesBottomBarWhenPushed = YES;
    UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    [(UINavigationController *)tabBarController.viewControllers[1] pushViewController:topicVC animated:YES];
//    NSLog(@"%@", [(UINavigationController *)tabBarController.viewControllers[2] viewControllers]);
}

#pragma makr - 夜间模式改变颜色
- (void)changeColor
{
    if (NSUSERDEFAULTS_NIGHT) {
        self.backgroundColor = BGCOLOR_BLACK;
        _postTimeLabel.textColor = [UIColor lightGrayColor];
        [_topicButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _contentLabel.textColor = [UIColor lightGrayColor];
        _visitCountField.textColor = [UIColor lightGrayColor];
    }
    else {
        self.backgroundColor = BGCOLOR_WHITE;
        _postTimeLabel.textColor = [UIColor grayColor];
        [_topicButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _contentLabel.textColor = [UIColor blackColor];
        _visitCountField.textColor = [UIColor grayColor];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
